<?php
/**
 * Created by PhpStorm.
 * User: 陈鸿
 * Date: 2016/5/17
 * Time: 17:51
 */

namespace QC;
use QC\Error;
use QC\Curl;

class QQ extends Error
{
    protected static $conf;

    protected static $appId;

    protected static $appKey;

    public static function setConf()
    {
        self::$conf   = include 'Conf.php';
        self::$appId  = self::getFiled('app','appId');
        self::$appKey = self::getFiled('app','appKey');
    }

    protected static function getFiled($filed,$value=null)
    {
        if (empty($value))
        {
            return self::$conf[$filed];
        } else {
            return self::$conf[$filed][$value];
        }
    }

    /**
     * QQ 登陆
     *
     * @param array $conf
     * @return string
     */
    public static function login($conf=[]){
        self::setConf();
        if ( !empty($conf) )
        {
            foreach ($conf as $key => $value)
            {
                if ( is_array($value) )
                {
                    foreach ($value as $item => $v)
                    {
                        self::$conf[$key][$item] = $v;
                    }
                } else {
                    self::$conf[$key] = $value;
                }
            }
            self::$appId = self::getFiled('app','appId');
            self::$appKey = self::getFiled('app','appKey');
        }

        $state = md5(uniqid(rand(), TRUE));
        //ZCache::getInstance()->set('qq_state',$state,10);
        $urlPram = [
            'response_type' => 'code',
            'client_id'     => self::$appId,
            'redirect_uri'  => self::getFiled('app','callback'),
            'state'         => $state,
            'scope'         => self::getFiled('app','scope')
        ];

        if ( !empty(self::getFiled('ext')) )
        {
            $urlPram['redirect_uri'] =  $urlPram['redirect_uri'] . '?' .  http_build_query(self::getFiled('ext'));
        }

        $loginUrl = self::getFiled('authUrl') . '?' .http_build_query($urlPram);
        return $loginUrl;
    }

    /**
     * 获取 essToken 和 openId
     *
     * @return mixed
     */
    public static function getAccessTokenAndOpenId($conf=null)
    {
        self::setConf();
        if ( !empty($conf) ) {
            foreach ($conf as $key => $value)
            {
                if ( is_array($value) )
                {
                    foreach ($value as $item => $v)
                    {
                        self::$conf[$key][$item] = $v;
                    }
                } else {
                    self::$conf[$key] = $value;
                }
            }
            self::$appId = self::getFiled('app','appId');
            self::$appKey = self::getFiled('app','appKey');
        }
        $code = $_GET['code'];
        $state = $_GET['state'];
        //$cahce = ZCache::getInstance()->get('qq_state');

        if ( $state)
        {
            //return self::get('30001');
        } else {

            $tokenParam = [
                'grant_type' => 'authorization_code',
                'client_id'  =>  self::$appId,
                'client_secret' => self::$appKey,
                'redirect_uri' => self::getFiled('app','callback'),
                'code'         => $code
            ];

            $tokenUrl = self::getFiled('accessTokenUrl') . '?' . http_build_query($tokenParam);
            $result   = Curl::get($tokenUrl);

            parse_str($result,$token);

            $openIdUrl    = self::getFiled('OpenIDUrl') . '?' . http_build_query(['access_token'=>$token['access_token']]);
            $openIdResult = Curl::get($openIdUrl);
            $openIdResult = str_replace('callback(','',$openIdResult);
            $openIdResult = str_replace(')','',$openIdResult);
            $openIdResult = str_replace(' ;','',$openIdResult);
            $openIdResult = json_decode($openIdResult,true);

            $data['access_token'] = $token['access_token'];
            $data['openId'] = $openIdResult['openid'];

            return self::get($data);
        }
    }

    /**
     * 获取用户信息
     *
     * @param $accessToken
     * @param $openId
     * @return mixed
     */
    public static function getUserInfo($accessToken,$openId)
    {
        $paramData = [
            'oauth_consumer_key' => self::$appId,
            'access_token'       => $accessToken,
            'openid'             => $openId,
            'format'             => 'json'
        ];

        $url = self::getFiled('userInfoUrl') . '?' . http_build_query($paramData);

        $json = Curl::get($url);

        return self::get(json_decode($json,true));
    }

    /**
     * 获取其他用户的详细信息
     *
     * @param $accessToken
     * @param $openId
     * @return mixed
     */
    public static function getOtherInfo($accessToken,$openId)
    {
        $paramData = [
            'oauth_consumer_key' => self::$appId,
            'access_token'       => $accessToken,
            'openid'             => $openId,
            'format'             => 'json'
        ];

        $url = self::getFiled('otherInfoUrl') . '?' . http_build_query($paramData);

        $json = Curl::get($url);

        return self::get(json_decode($json,true));
    }

    /**
     * 获取登录用户自己的详细信息
     *
     * @param $accessToken
     * @param $openId
     * @return mixed
     */
    public static function getInfo($accessToken,$openId)
    {
        $paramData = [
            'oauth_consumer_key' => self::$appId,
            'access_token'       => $accessToken,
            'openid'             => $openId,
            'format'             => 'json'
        ];

        $url = self::getFiled('infoUrl') . '?' . http_build_query($paramData);

        $json = Curl::get($url);

        return self::get(json_decode($json,true));
    }
}